package com.orm.handler;

import java.sql.*;
import java.time.Instant;
import java.util.Date;


/**
 *
 */
public class InstantTypeHandler extends BaseTypeHandler<Instant> {
	@Override
	public void setNotNullParameter(PreparedStatement ps, int i, Instant parameter, JDBCType jdbcType) throws SQLException {
		ps.setTimestamp(i, Timestamp.from(parameter));
	}
	
	@Override
	public Instant getNullableResult(ResultSet rs, String columnName) throws SQLException {
		Timestamp timestamp = rs.getTimestamp(columnName);
		return getInstant(timestamp);
	}
	
	@Override
	public Instant getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
		Timestamp timestamp = rs.getTimestamp(columnIndex);
		return getInstant(timestamp);
	}
	
	public Instant getInstant(Timestamp timestamp){
		if(timestamp != null){
			return timestamp.toInstant();
		}
		return null;
	}
}
